// pages/my_content/test_record/test_record.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    y_m_d_t : [ ],
    layout_height : 0,
  },

  jump_rescord(e){
    console.log(e.currentTarget.id)
    const db = wx.cloud.database()
    const todo = db.collection('record-test')
    todo.where({ _id : e.currentTarget.id }).get({
      success: function (res) {
        console.log(res.data[0])
        wx.navigateTo({
          url: `/package_facial_analysis/pages/statement/statement?id=${res.data[0].navid}`,
        })
      }
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let that = this;

    var userID = options.userID;
    var recordCount = options.recordCount;
    const sysinfo = wx.getSystemInfoSync()
    that.setData({
      layout_height: sysinfo.screenHeight,
    })
    
    var loopCount = recordCount/20;
    const recordTemp = [];
    for (var k = 0; k <= loopCount; k++){
      const db = wx.cloud.database()
      const todo = db.collection('record-test')
      await todo.where({ id_user : userID }).skip(k * 20).get().then(res => {
        recordTemp.push(...res.data);
        var flagEnd = k + 1;
        if ( flagEnd > loopCount ){
          var temp = that.integrateDate(recordTemp);
          that.setData({y_m_d_t : temp})
        }
      })
    }
  },

  transformData(data) {  
    const result = [];  
    data.sort((a, b) =>{ 
      if (a.year !== b.year) {  
          return b.year - a.year;
      }   
      if (a.month !== b.month) {  
          return b.month - a.month; 
      }  
      return a.day - b.day; 
    })

    for (let item of data) {  
      const transformedItem = {
          year : item.year,
          m_d_t : [{
              month: item.month,
              d_t : [{
                  day : item.day,
                  t : [{time: item.time, result : item.eye_shape, id : item._id} ]
              }]
          }]
      };  
      result.push(transformedItem);  
    }  
    return result;  
  },

  integrateDate(date) {
    let that = this;
    var temp = that.transformData(date)
    for (var i = 1; i < temp.length; i++){
      if (temp[i].year == temp[i - 1].year){
        var m_end = temp[i - 1].m_d_t.length - 1
        if (temp[i].m_d_t[0].month == temp[i - 1].m_d_t[m_end].month){
          var d_end = temp[i - 1].m_d_t[m_end].d_t.length - 1
          if (temp[i].m_d_t[0].d_t[0].day == temp[i - 1].m_d_t[m_end].d_t[d_end].day){
            temp[i - 1].m_d_t[m_end].d_t[d_end].t.push(...temp[i].m_d_t[0].d_t[0].t)
            temp.splice(i,1)
          }else{
            temp[i - 1].m_d_t[m_end].d_t.push(...temp[i].m_d_t[0].d_t)
            temp.splice(i,1)
          }
        }else{
          temp[i - 1].m_d_t.push(...temp[i].m_d_t)
          temp.splice(i,1)
        }
        i--
      }
    }
    return temp;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})